//
//  LDY_ReadDetailTableViewCell.m
//  ProjectOfBStage
//
//  Created by dlios on 15/11/16.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "LDY_ReadDetailTableViewCell.h"
#import <SDImageCache.h>
#import <UIImageView+WebCache.h>

@implementation LDY_ReadDetailTableViewCell

- (void)dealloc
{
    [_titleLabel release];
    [_picImageView release];
    [_contentLabel release];
    [_model release];
    [_title release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [self.titleLabel release];
        
        self.picImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.picImageView];
        [self.picImageView release];
        
        self.contentLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.contentLabel];
        [self.contentLabel release];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat height =[[self class] heightOfLabel:self.title andWith:self.contentView.frame.size.width];
    
    self.titleLabel.frame = CGRectMake(KWIDTH * 10, KHEIGHT * 5, self.contentView.frame.size.width - KWIDTH * 10, height + KWIDTH * 30);
    self.titleLabel.font = [UIFont systemFontOfSize:20 * KWIDTH];
    self.titleLabel.numberOfLines = 0;
    
    self.picImageView.frame = CGRectMake(KWIDTH * 10, self.titleLabel.frame.size.height + KHEIGHT * 20, KWIDTH * 120, KHEIGHT * 60);
    
    self.contentLabel.frame = CGRectMake(KWIDTH * 15 + self.picImageView.frame.size.width, self.titleLabel.frame.size.height + KHEIGHT * 20, self.contentView.frame.size.width - self.picImageView.frame.size.width - KWIDTH * 20, self.contentView.frame.size.height - self.picImageView.frame.size.height - KHEIGHT * 40);
    self.contentLabel.font = [UIFont systemFontOfSize:13 * KWIDTH];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = [UIColor grayColor];
}



- (void)setModel:(LDY_ReadDetailModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    self.title = model.title;
    
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:[UIImage imageNamed:@"wait"]];
    
    self.contentLabel.text = model.content;
}


- (void)setTitle:(NSString *)title
{
    if (_title != title) {
        [_title release];
        _title = [title retain];
    }
    
    self.titleLabel.text = self.title;
}


/*
 * 根据要显示的内容计算:label的frame, 并且返回高度值
 */
+ (CGFloat)heightOfLabel:(NSString *)text andWith:(CGFloat)width
{
    //第一步: 设置文本的最大显示范围
    CGSize size = CGSizeMake(width, MAXFLOAT);
    
    //第二步: 设置字体样式
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    
    //
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
